package cn.xz.zysjz.utils;

import java.util.ArrayList;
import java.util.Objects;

import cn.xz.zysjz.bean.AppData;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.HomeListBean;
import cn.xz.zysjz.bean.PFBean;
import cn.xz.zysjz.bean.PLBean;
import cn.xz.zysjz.bean.UserBean;

/**
 * 获取数据*/
public class GetDataUtils {

    //获取数据
    public static ArrayList<HomeListBean> get(int i){
        ArrayList<HomeListBean> mList = new ArrayList<>();
        switch (i){
            case 0://全部
                ArrayList<HomeListBean> list = SessionUtils.getList(CommonLib.ADD_YS, HomeListBean.class);
                ArrayList<HomeListBean> ml1 = (ArrayList<HomeListBean>) AppHelper.jsonToList(AppData.mVac,HomeListBean.class);
                ArrayList<HomeListBean> ml2 = (ArrayList<HomeListBean>) AppHelper.jsonToList(AppData.mVFood,HomeListBean.class);
                ArrayList<HomeListBean> ml3 = (ArrayList<HomeListBean>) AppHelper.jsonToList(AppData.mVSlp,HomeListBean.class);
                ArrayList<HomeListBean> ml4 = (ArrayList<HomeListBean>) AppHelper.jsonToList(AppData.mVMin,HomeListBean.class);
                mList.addAll(list);
                mList.addAll(ml1);
                mList.addAll(ml2);
                mList.addAll(ml3);
                mList.addAll(ml4);
                break;
            case 1://景点
                ArrayList<HomeListBean> jList = SessionUtils.getList(CommonLib.ADD_YS, HomeListBean.class);
                for (HomeListBean homeListBean: jList){
                    if (homeListBean.getType() == 1){
                        mList.add(homeListBean);
                    }
                }
                ArrayList<HomeListBean> mJl1 = (ArrayList<HomeListBean>) AppHelper.jsonToList(AppData.mVac,HomeListBean.class);
                mList.addAll(mJl1);
                break;
            case 2://美食
                ArrayList<HomeListBean> msList = SessionUtils.getList(CommonLib.ADD_YS, HomeListBean.class);
                for (HomeListBean homeListBean: msList){
                    if (homeListBean.getType() == 2){
                        mList.add(homeListBean);
                    }
                }
                ArrayList<HomeListBean> msl1 = (ArrayList<HomeListBean>) AppHelper.jsonToList(AppData.mVFood,HomeListBean.class);
                mList.addAll(msl1);
                break;
            case 3://酒店
                ArrayList<HomeListBean> jdList = SessionUtils.getList(CommonLib.ADD_YS, HomeListBean.class);
                for (HomeListBean homeListBean: jdList){
                    if (homeListBean.getType() == 3){
                        mList.add(homeListBean);
                    }
                }
                ArrayList<HomeListBean> jdl1 = (ArrayList<HomeListBean>) AppHelper.jsonToList(AppData.mVSlp,HomeListBean.class);
                mList.addAll(jdl1);
                break;
            case 4://民宿
                ArrayList<HomeListBean> mssList = SessionUtils.getList(CommonLib.ADD_YS, HomeListBean.class);
                for (HomeListBean homeListBean: mssList){
                    if (homeListBean.getType() == 4){
                        mList.add(homeListBean);
                    }
                }
                ArrayList<HomeListBean> mdsl1 = (ArrayList<HomeListBean>) AppHelper.jsonToList(AppData.mVMin,HomeListBean.class);
                mList.addAll(mdsl1);
                break;
            default:
                break;
        }
        //过滤评分
        for (HomeListBean homeListBean: mList){
            homeListBean.setGet(homeListBean.getGet() + getPf(homeListBean.getNum()));
        }
        return mList;
    }

    //获取指定数据的评分
    public static int getPf(int i){
        int p = 0;
        ArrayList<PFBean> list = SessionUtils.getList(CommonLib.PF_LIST, PFBean.class);
        for (PFBean pfBean: list){
            if (pfBean.getNum() == i){
                p = pfBean.getPrice();
            }
        }
        return p;
    }

    //是否评过分
    public static boolean setPf(int i){
        boolean p = false;
        ArrayList<PFBean> list = SessionUtils.getList(CommonLib.PF_LIST, PFBean.class);
        for (PFBean pfBean: list){
            if (pfBean.getNum() == i){
                p = true;
            }
        }
        return p;
    }

    //删除内容
    public static void delData(int o){
        ArrayList<HomeListBean> list = SessionUtils.getList(CommonLib.ADD_YS, HomeListBean.class);
        int position = -1;
        for (int i = 0; i < list.size(); i ++){
            if (list.get(i).getNum() == o){
                position = i;
            }
        }
        if (position != -1){
            list.remove(position);
        }
        SessionUtils.commitList(CommonLib.ADD_YS,list);

        //删除的时候，收藏列表里也删除
        ArrayList<HomeListBean> scList = SessionUtils.getList(CommonLib.SC_LIST, HomeListBean.class);
        int mP = -1;
        for (int i=0;i<scList.size();i++){
            if (scList.get(i).getNum() == o){
                mP = i;
            }
        }
        if (mP != -1){
            scList.remove(mP);
            SessionUtils.commitList(CommonLib.SC_LIST,scList);
        }

    }

    //修改内容
    public static void upData(int o, String s){
        ArrayList<HomeListBean> list = SessionUtils.getList(CommonLib.ADD_YS, HomeListBean.class);
        for (int i = 0; i < list.size(); i ++){
            if (list.get(i).getNum() == o){
               list.get(i).setText(s);
            }
        }
        SessionUtils.commitList(CommonLib.ADD_YS,list);
    }

    //获取说明内容
    public static String getSM(int o){
        String s = "暂无说明";
        ArrayList<HomeListBean> homeListBeans = get(0);
        for (HomeListBean homeListBean: homeListBeans){
            if (homeListBean.getNum() == o){
                s = homeListBean.getText();
            }
        }
        return s;
    }

    //获取当前登录用户
    public static UserBean getUser(String name){
        UserBean mR = null;
        ArrayList<UserBean> userList = SessionUtils.getList(CommonLib.USER_DATA, UserBean.class);
        for(UserBean userBean: userList){
            if (Objects.equals(userBean.getName(), name)){
                mR = userBean;
            }
        }
        return mR;
    }


    //添加评论
    public static void inputPL(PLBean plBean){
        ArrayList<PLBean> list = SessionUtils.getList(CommonLib.PL_LIST, PLBean.class);
        list.add(plBean);
        SessionUtils.commitList(CommonLib.PL_LIST,list);
    }

    //删除评论
    public static void delPL(int num,String txt){
        int y = -1;
        ArrayList<PLBean> list = SessionUtils.getList(CommonLib.PL_LIST, PLBean.class);
        for (int i=0;i<list.size();i++){
            if (list.get(i).getNum() == num && list.get(i).getTxt().contains(txt)){
                y = i;
            }
        }
        if (y != -1){
            list.remove(y);
            SessionUtils.commitList(CommonLib.PL_LIST,list);
        }else {
            ToastUtils.show("未检索到该关键字");
        }
    }

    //获取评论
    public static ArrayList<PLBean> getPL(int num){
         ArrayList<PLBean> rlist = new ArrayList<>();
         ArrayList<PLBean> list = SessionUtils.getList(CommonLib.PL_LIST, PLBean.class);
         for (PLBean plBean: list){
             if (plBean.getNum() == num){
                 rlist.add(plBean);
             }
         }
         return rlist;
    }

}
